﻿@using PagedList.Mvc
@using PagedList
@using TestOnlineProject.Common
@using TestOnlineProject.Models.Entities
<script>
    $(document).ready(function () {
        // Select all.
        $("#select_all").click(function () {
            $(".is_selected").attr('checked', true);
        });

        // Unselect all.
        $("#unselect_all").click(function () {
            $(".is_selected").attr('checked', false);
        });

        $("#deactivate").click(function() {
            var con = confirm("Are you sure you want to deactivate this question(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function(i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one question!")
                } else {
                    // Deactivate selected item.
                    $.ajax({
                        url: "@Url.Content("~/Question/Deactivate")?ids=" + val,
                        type: 'GET',
                        success: function(data) {
                            if (data != null) {
                                alert(data);
                                // Reload page with keep page states.
                                var flag = $("input[name=import]").val();
                                var content;
                                if (flag >= 0) {
                                    content = "@Url.Content("~/Question/ImportResult")?sectionId=" + flag;
                                } else {
                                    content = "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId";
                                }
                                $.ajax({
                                    url: content,
                                    //url: "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#activate").click(function() {
            var con = confirm("Are you sure you want to activate this question(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function(i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one question!")
                } else {
                    // Activate selected item.
                    $.ajax({
                        url: "@Url.Content("~/Question/Activate")?ids=" + val,
                        type: 'GET',
                        success: function(data) {
                            if (data != null) {
                                alert(data);
                                // Reload page with keep page states.
                                var flag = $("input[name=import]").val();
                                var content;
                                if (flag >= 0) {
                                    content = "@Url.Content("~/Question/ImportResult")?sectionId=" + flag;
                                } else {
                                    content = "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId";
                                }
                                $.ajax({
                                    url: content,
                                    //url: "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId",
                                    type: 'GET',
                                    success: function(data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#delete").click(function () {
            var con = confirm("Are you sure you want to delete this question(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one question!")
                }
                else {
                    // Delete selected item.
                    $.ajax({
                        url: "@Url.Content("~/Question/Delete")?ids=" + val,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload page with keep page states.
                                var flag = $("input[name=import]").val();
                                var content;
                                if (flag >= 0) {
                                    content = "@Url.Content("~/Question/ImportResult")?sectionId=" + flag;
                                } else {
                                    content = "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId";
                                }
                                $.ajax({
                                    url: content,
                                    //url: "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    });

    function AddQuestions(sectionId) {
        var val = "";
        $(':checkbox:checked').each(function (i) {
            val += $(this).val() + "-";
        });
        if (val == "") {
            alert("You must select at least one question!");
        }
        else {
            // Add Question to Section selected item.
            $.ajax({
                url: "@Url.Content("~/Section/AddQuestions")?sectionId=" + sectionId + "&ids=" + val,
                type: 'GET',
                success: function(data) {
                    if (data != null) {
                        alert(data);
                        // Reload page with keep page states.
                        var flag = $("input[name=import]").val();
                        var content;
                        if (flag >= 0) {
                            content = "@Url.Content("~/Question/ImportResult")?sectionId=" + flag;
                        } else {
                            content = "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId";
                        }
                        $.ajax({
                            url: content,
                            //url: "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId",
                            type: 'GET',
                            success: function(data) {
                                $("#unobtrusive").html(data);
                            }
                        });
                    }
                }
            });
        }
    }

    function RemoveQuestions(sectionId) {
        var val = "";
        $(':checkbox:checked').each(function (i) {
            val += $(this).val() + "-";
        });
        if (val == "") {
            alert("You must select at least one question!");
        }
        else {
            // Remove Question from Section selected item.
            $.ajax({
                url: "@Url.Content("~/Section/RemoveQuestions")?sectionId=" + sectionId + "&ids=" + val,
                type: 'GET',
                success: function(data) {
                    if (data != null) {
                        alert(data);
                        // Reload page with keep page states.
                        var flag = $("input[name=import]").val();
                        var content;
                        if (flag >= 0) {
                            content = "@Url.Content("~/Question/ImportResult")?sectionId=" + flag;
                        } else {
                            content = "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId";
                        }
                        $.ajax({
                            url: content,
                            //url: "@Url.Content("~/Question/Index")?page=@ViewBag.Page&createdBy=@ViewBag.CreatedBy&questionTypeId=@ViewBag.QuestionTypeId&actualUser=@ViewBag.ActualUser&subjectId=@ViewBag.Subject.SubjectId&column=@ViewBag.Column&sort=@ViewBag.Sort&keyWord=@ViewBag.KeyWord&sectionId=@ViewBag.SectionId",
                            type: 'GET',
                            success: function(data) {
                                $("#unobtrusive").html(data);
                            }
                        });
                    }
                }
            });
        }
    }

</script>

<div id="list">
    <div>
        <table class="table table-hover" style="table-layout: fixed">
            <tr>
                <th style="vertical-align: middle; width: 50px">
                    @Ajax.ActionLink("No.", "Index", new { column = "No", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "No") ? "Desc" : "Asc", createdBy = @ViewBag.CreatedBy, actualUser = @ViewBag.ActualUser, subjectId = @ViewBag.Subject.SubjectId, keyWord = ViewBag.KeyWord, sectionId = ViewBag.SectionId, questionTypeId = @ViewBag.QuestionTypeId }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle">
                    @Ajax.ActionLink("Question's Title", "Index", new { column = "Title", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "Title") ? "Desc" : "Asc", createdBy = @ViewBag.CreatedBy, actualUser = @ViewBag.ActualUser, subjectId = @ViewBag.Subject.SubjectId, keyWord = ViewBag.KeyWord, sectionId = ViewBag.SectionId, questionTypeId = @ViewBag.QuestionTypeId }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle; width: 100px">
                    @Ajax.ActionLink("Type", "Index", new { column = "QuestionType", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "QuestionType") ? "Desc" : "Asc", createdBy = @ViewBag.CreatedBy, actualUser = @ViewBag.ActualUser, subjectId = @ViewBag.Subject.SubjectId, keyWord = ViewBag.KeyWord, sectionId = ViewBag.SectionId, questionTypeId = @ViewBag.QuestionTypeId }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="width: 30px">
                    <div class="btn-group">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu pull-right">
                            <li>
                                <button class="btn btn-link" id="select_all">
                                    Select All</button></li>
                            <li>
                                <button class="btn btn-link" id="unselect_all">
                                    Unselect All</button></li>
                            @{
                                if (HttpContext.Current.User.IsInRole("1"))
                                {
                                    <li>
                                        <button class="btn btn-link" id="deactivate">
                                            Deactivate</button></li>
                                    <li>
                                        <button class="btn btn-link" id="activate">
                                            Activate</button></li>
                                }
                            }
                            <li>
                                <button class="btn btn-link" id="delete">
                                    Delete</button></li>
                            <li class="dropdown-submenu pull-left">
                                <button class="btn btn-link" id="add_to_section">
                                    Add to Section</button>
                                <ul class="dropdown-menu pull-right" style="width: 150px">
                                    @{
                                        if ((ViewBag.SectionList as List<Section>).Count > 0)
                                        {
                                            foreach (var item in ViewBag.SectionList as List<Section>)
                                            {
                                                <li>
                                                    <button style="white-space: normal;word-break:break-all;text-align:left" class="add_to_section btn btn-link" onclick="AddQuestions('@item.SectionId')">
                                                        +&nbsp;@item.Name</button></li>
                                            }
                                        }
                                        else
                                        {
                                            <li><span>&nbsp;No section.</span></li>
                                        }
                                    }
                                </ul>
                            </li>
                            <li class="dropdown-submenu pull-left">
                                <button class="btn btn-link" id="add_to_section">
                                    Remove from Section</button>
                                <ul class="dropdown-menu pull-right" style="width: 150px">
                                    @{
                                        if ((ViewBag.SectionList as List<Section>).Count > 0)
                                        {
                                            foreach (var item in ViewBag.SectionList as List<Section>)
                                            {
                                                <li>
                                                    <button style="white-space: normal;word-break:break-all;text-align:left" class="add_to_section btn btn-link" onclick="RemoveQuestions('@item.SectionId')">
                                                        +&nbsp;@item.Name</button></li>
                                            }
                                        }
                                        else
                                        {
                                            <li><span>&nbsp;No section.</span></li>
                                        }
                                    }
                                </ul>
                            </li>
                        </ul>
                    </div>
                </th>
            </tr>
            @{
                int row = 0;
                foreach (var item in ViewBag.onePageOfQuestions as IEnumerable<Question>)
                {
                    row++;
                    <tr id="row_@row">
                        <td>
                            @{
                                var noList = ViewBag.NoList as List<NoModel>;
                                var no = noList.Where(n => n.Id == item.QuestionId).FirstOrDefault().No;
                                <span style="word-wrap: break-word">@no</span>
                            }
                        </td>
                        <td>
                            @{
                                <span style="word-wrap: break-word">@Html.ActionLink(item.Title, "Details", new { id = item.QuestionId, sectionId = (ViewBag.Section != null ? ViewBag.Section.SectionId : 0) }, null)</span>
                            }
                        </td>
                        <td>
                            @item.QuestionType.Name
                        </td>
                        <td>
                            <input type="checkbox" value="@item.QuestionId" class="is_selected"/>
                        </td>
                    </tr>
                
                    if (item.UserId == int.Parse(HttpContext.Current.User.Identity.Name))
                    {
                        <script>
                            $("#row_@row").addClass("mine");
                        </script>
                    }
                    if (!item.IsActivated)
                    {
                        <script>$("#row_@row").addClass("deactivated_item"); </script>
                    }
                }
                
            }
        </table>
    </div>
    @try
    {
        if (ViewBag.IsImportResultPage != null && ViewBag.IsImportResultPage == true)
        {
            @Html.PagedListPager((IPagedList)ViewBag.onePageOfQuestions, page => Url.Action("ImportResult", new { page, size = 10 }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "unobtrusive", OnComplete = "" }))
        }
        else
        {
            @Html.PagedListPager((IPagedList)ViewBag.onePageOfQuestions, page => Url.Action("Index", new { page, sort = @ViewBag.Sort, column = @ViewBag.Column, createdBy = @ViewBag.CreatedBy, actualUser = @ViewBag.ActualUser, subjectId = @ViewBag.Subject.SubjectId, keyWord = ViewBag.KeyWord, sectionId = ViewBag.SectionId, questionTypeId = @ViewBag.QuestionTypeId }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "unobtrusive", OnComplete = "" }))
        }
    }
    catch (Exception ex)
    {
    }
</div>